package com.zx.account.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zx.account.dao.MonthMapper;
import com.zx.account.entity.Month;
import com.zx.account.service.MonthService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Slf4j
@Service
public class MonthServiceImpl implements MonthService {

    @Autowired
    private MonthMapper monthMapper;

    @Override
    public boolean insert(int year, int month, int userId) {
        log.info("MonthService#insert year={} month={} userId={}", year, month, userId);
        Month mon = new Month().setAYear(year).setAMonth(month).setUserId(userId);
        return this.monthMapper.insert(mon) > 0;
    }

    @Override
    public boolean getEmpty(int year, int month, int userId) {
        log.info("MonthService#getEmpty year={} month={} userId={}", year, month, userId);
        List<Month> data = this.monthMapper.selectList(
                Wrappers.<Month>lambdaQuery()
                        .eq(Month::getAYear, year)
                        .eq(Month::getAMonth, month)
                        .eq(Month::getUserId, userId)
        );
        log.info("MonthService#getEmpty data.size={}", data.size());
        return CollectionUtils.isEmpty(data);
    }

    @Override
    public List<Month> getByUserId(int userId) {
        log.info("MonthService#getByUserId userId={}", userId);
        return this.monthMapper.selectList(Wrappers.<Month>lambdaQuery()
                .eq(Month::getUserId, userId));
    }

    @Override
    public Month getById(int id) {
        log.info("MonthService#getById id={}", id);
        return this.monthMapper.selectById(id);
    }
}
